//
//  ByApiAppIndex.m
//  chequanbao_ios
//
//  Created by pro on 2017/6/28.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByApiAppIndex.h"

// model
#import "ByModelIndexCarousel.h"

@implementation ByApiAppIndex

// 获取首页轮播
+ (void)getIndexCarousel:(ByNetParamsModel *)model
                 success:(void(^)(ByNetBaseResult *result))success
                 failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"By_Banners_query";
    static NSInteger apiVersion = 100;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.position forKey:@"position"];
    
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeArray
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"获取成功";
           NSMutableArray *temp = [NSMutableArray new];
           [((NSArray<NSDictionary *> *)responseObject) enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
               
               ByModelIndexCarousel *model = [[ByModelIndexCarousel alloc] initContentWithDic:obj];
               [temp addObject:model];
           }];
           result.data = temp.copy;
           result.notify_id = notifyID;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           DLog(@"******** %@ 获取首页轮播: %@", apiType, error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];
    

}
@end
